package com.ruoyi.caiwu.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.caiwu.domain.AccountingVoucher;
import com.ruoyi.caiwu.domain.Accounts;
import com.ruoyi.caiwu.service.IAccountingVoucherService;
import com.ruoyi.caiwu.service.IAccountsService;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 凭证Controller
 *
 * @author ruoyi
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/voucher")
public class AccountingVoucherController extends BaseController
{
    @Autowired
    private IAccountingVoucherService accountingVoucherService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IAccountsService accountsService;

    /**
     * 查询凭证列表
     */
    @RequiresPermissions("caiwu:voucher:list")
    @GetMapping("/list")
    public TableDataInfo list(AccountingVoucher accountingVoucher)
    {
        startPage();
        List<AccountingVoucher> list = accountingVoucherService.selectAccountingVoucherList(accountingVoucher);
        return getDataTable(list);
    }
    /**
     * 查询账套列表
     */
    @RequiresPermissions("caiwu:voucher:list")
    @GetMapping("/accounts")
   public List<Accounts> getAccountById() {
        return accountsService.selectAccounts();
   }



    /**
     * 导出凭证列表
     */
    @RequiresPermissions("caiwu:voucher:export")
    @Log(title = "凭证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountingVoucher accountingVoucher)
    {
        List<AccountingVoucher> list = accountingVoucherService.selectAccountingVoucherList(accountingVoucher);
        ExcelUtil<AccountingVoucher> util = new ExcelUtil<AccountingVoucher>(AccountingVoucher.class);
        util.exportExcel(response, list, "凭证数据");
    }


        /**
         * 导入凭证数据
         */
        @RequiresPermissions("caiwu:voucher:import")
        @Log(title = "凭证", businessType = BusinessType.IMPORT)
        @PostMapping("/import")
        public AjaxResult importData(@RequestParam("file") MultipartFile file) {
            try {
                // 使用 ExcelUtil 工具类读取上传的 Excel 文件
                ExcelUtil<AccountingVoucher> util = new ExcelUtil<>(AccountingVoucher.class);
                List<AccountingVoucher> voucherList = util.importExcel(file.getInputStream());
                // 调用服务层的方法将数据保存到数据库
                    int result = accountingVoucherService.importAccountingVouchers(voucherList);
                    return AjaxResult.success("导入成功，共导入 " + result + " 条数据");
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error("导入失败：" + e.getMessage());
            }
        }

    /**
     * 获取凭证详细信息
     */
    @RequiresPermissions("caiwu:voucher:query")
    @GetMapping(value = "/{voucherId}")
    public AjaxResult getInfo(@PathVariable("voucherId") Long voucherId)
    {
        return success(accountingVoucherService.selectAccountingVoucherByVoucherId(voucherId));
    }

    /**
     * 新增凭证
     */
    @RequiresPermissions("caiwu:voucher:add")
    @Log(title = "凭证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody AccountingVoucher accountingVoucher)
    {
        return toAjax(accountingVoucherService.insertAccountingVoucher(accountingVoucher));
    }

    /**
     *  红冲凭证
     */
    @RequiresPermissions("caiwu:voucher:edit")
    @Log(title = "凭证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AccountingVoucher accountingVoucher)
    {
        // 插入冲销凭证到数据库
        accountingVoucherService.insertAccounting(accountingVoucher);
        return success();
    }

    /*
     * 审核
     */
    @RequiresPermissions("caiwu:voucher:edit")
    @Log(title = "凭证",businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult audit(@Validated @PathVariable("id") Long id)
    {
        // 获取当前登录用户信息
        LoginUser loginUser = tokenService.getLoginUser();
        String s = stringRedisTemplate.opsForValue().get("user:" + loginUser.getUserid());
        if (s != null && s.equals("普通角色")) {
            return AjaxResult.error("普通角色无权审核");
        }
        return toAjax(accountingVoucherService.audit(id));
    }
    /**
     * 删除凭证
     */
    @RequiresPermissions("caiwu:voucher:remove")
    @Log(title = "凭证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{voucherIds}")
    public AjaxResult remove(@PathVariable Long[] voucherIds)
    {
        return toAjax(accountingVoucherService.deleteAccountingVoucherByVoucherIds(voucherIds));
    }
}
